<template>
  <div>
    <div v-for="(item, index) in list" :key="index">
      <div v-if="item.article.hasPoster" class="item">
        <div class="content">
          <router-link :to="'/article/details/' + item.article.id">
            <div style="display: flex">
              <div>
                <div class="author">
                  <van-image
                    round
                    width="30"
                    height="30"
                    :src="base + '/file/image/' + item.author.avatar"
                  ></van-image>
                  <div>
                    <div>
                      {{ item.author.nickname }}
                    </div>
                    <div v-if="LocalRul == '/me'" style="float: right">
                      <div>
                        <div v-if="item.article.checker == '0'" class="status">
                          <van-button size="mini" type="info">
                            待审核</van-button
                          >
                        </div>
                        <div v-if="item.article.checker == '1'" class="status">
                          <van-button size="mini" type="primary">
                            审核已通过</van-button
                          >
                        </div>
                        <div v-if="item.article.checker == '2'" class="status">
                          <van-button size="mini" type="danger">
                            审核未通过</van-button
                          >
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="title">{{ item.article.title }}</div>
            <div class="article-poster">
              <van-image
                width="100%"
                height="auto"
                fit="contain"
                :src="base + '/file/image/' + item.article.poster"
              />
            </div>
          </router-link>
          <div class="star-bar">
            <span class="star-bar-count"
              >{{ item.article.agreeCount }}人赞同 ·
              {{ item.article.commentCount }}条评论</span
            >
            <div
              style="float: right; margin-right: 0px"
              v-if="LocalRul == '/me'"
            >
              <van-button
                type="danger"
                round
                size="mini"
                @click="deleteArticle(item.article.id)"
                >删除文章</van-button
              >
            </div>
          </div>
        </div>
      </div>

      <div v-if="!item.article.hasPoster" class="item">
        <div class="content">
          <router-link :to="'/article/details/' + item.article.id">
            <div>
              <div class="author">
                <van-image
                  round
                  width="30"
                  height="30"
                  :src="base + '/file/image/' + item.author.avatar"
                />
                <div>
                  <div>
                    {{ item.author.nickname }}
                  </div>
                  <div v-if="LocalRul == '/me'" style="float: right">
                    <div v-if="item.article.checker == '0'" class="status">
                      <van-button size="mini" type="info"> 待审核</van-button>
                    </div>
                    <div v-if="item.article.checker == '1'" class="status">
                      <van-button size="mini" type="primary">
                        审核已通过</van-button
                      >
                    </div>
                    <div v-if="item.article.checker == '2'" class="status">
                      <van-button size="mini" type="danger">
                        审核未通过</van-button
                      >
                    </div>
                  </div>
                </div>
              </div>
              <div class="title">{{ item.article.title }}</div>
            </div>
          </router-link>
          <div class="star-bar">
            <span class="star-bar-count"
              >{{ item.article.agreeCount }}人赞同 ·
              {{ item.article.commentCount }}条评论</span
            >

            <div
              style="float: right; margin-right: 0px"
              v-if="LocalRul == '/me'"
            >
              <van-button
                type="danger"
                round
                size="mini"
                @click="deleteArticle(item.article.id)"
                >删除文章</van-button
              >
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { BASE_RUL } from "@/utils/request";
import { DeleteArticle } from "@/api/article";
import { Dialog } from "vant";

export default {
  name: "ArticleList",
  data() {
    return {
      base: BASE_RUL,
      LocalRul: this.$route.path,
      aid: "",
    };
  },
  methods: {
    deleteArticle(id) {
      this.$dialog
        .confirm({
          message: "确认删除?",
          confirmButtonColor: "red",
        })
        .then(() => {
          console.log(id);
          DeleteArticle(id).then((res) => {
            if (res.status) {
              this.$toast.success("删除成功");
              this.$router.go(0);
            }
          });
        })
        .catch(() => {});
    },
  },
  props: {
    list: {
      type: Array,
    },
  },
};
</script>

<style scoped>
.status {
  text-align: center;
  line-height: 15px;
  font-family: 微软雅黑;
  font-size: 15px;
}
.article-poster {
  min-width: 100px;
  min-height: 63px;
  border-radius: 5px;
  margin: 15px 0 0 10px;
  object-fit: fill;
}

.item {
  width: 100%;
  background: #ffffff;
  margin-bottom: 10px;
}

.content {
  padding: 10px;
}

.title {
  letter-spacing: 1px;
  font-weight: 500;
  color: rgba(0, 0, 0, 0.9);
}

.author {
  display: flex;
  padding-top: 10px;
}

.author-name {
  padding-left: 10px;
  font-size: 12px;
  letter-spacing: 1px;
  color: rgba(0, 0, 0, 0.9);
}

.author-des {
  padding-left: 3px;
  font-size: 12px;
  letter-spacing: 1px;
  color: rgba(0, 0, 0, 0.6);
}

.article {
  font-size: 14px;
  letter-spacing: 1px;
  padding: 5px 0;
  color: rgba(0, 0, 0, 0.8);
  max-height: 35px;

  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.star-bar {
  height: 25px;
  padding-top: 8px;
}

.star-bar-count {
  font-size: 13px;
  letter-spacing: 2px;
  color: rgba(0, 0, 0, 0.5);
}
</style>